package sfin.swt.composite;

import java.util.ResourceBundle;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import sfin.controller.LancamentoController;
import sfin.swt.data.DateData;
import sfin.swt.exception.StatizoException;
import sfin.swt.util.DialogUtil;
import sfin.swt.util.MessageUtil;
import sfin.swt.util.ResourceUtil;

public class LancamentoComposite extends Composite implements DisposeListener{

	private ResourceBundle bundle;
	private LancamentoController controller;

	private DateTime dtPgto;
	private DateTime dtDiv;
	private Combo cbCategorias;
	private Text txtDescr;
	private Text txtVal;
	private Combo cbConta;
	private Combo cbFormaPgto;
	private Combo cbCartao;
	private Text txtNumPar;
	private Button chkPg;
	private Button btConfCartao;
	private Button btnSave;
	private Button btnConfig1;
	private Button btnConfig2;
	private Button chkMesmoValor;
	private Button chkDividir;
	private Label lblCartao;
	private Label lblContaTransf;
	private Combo cbContaTransf;
	private Button btConfContaTransf;
	
	private DialogUtil dialogUtil;
	private MessageUtil messageUtil;
	private Button chkVinc;
	private Button btHelpChkVinc;
	private Button chkFix;
	
	public LancamentoComposite(Composite parent,int style, LancamentoController controller,ResourceBundle bundle, MessageUtil messageUtil, DialogUtil dialogUtil) {
		super(parent, style);
		this.bundle = bundle;
		this.controller = controller;
		this.dialogUtil = dialogUtil;
		this.messageUtil = messageUtil;
		initialize();
		fillData();
	}

	protected void checkParc(){
		boolean editing = controller.isEditing();
		chkMesmoValor.setEnabled(!editing);
		chkDividir.setEnabled(!editing);		
	}

	protected void fillData(){
		
		boolean loadingPartial = controller.isLoadingPartial();
		boolean pago = controller.isPago();
		boolean editingParcel = controller.isParcelado() && controller.isEditing();
		
		if(!loadingPartial){
			
			DateData date = controller.getDataPgto();
			dtPgto.setDate(date.year, date.month, date.day);

			date = controller.getDataDiv();
			dtDiv.setDate(date.year, date.month, date.day);

			txtDescr.setText(controller.getDescricao());
			txtVal.setText(controller.getValor());
			chkPg.setSelection(controller.isPago());
			chkVinc.setSelection(controller.isVincular());
			chkFix.setSelection(controller.isFixPgDt());
			
		}
		
		if(!loadingPartial || controller.isCategoriaPartialLoading()){
			cbCategorias.setItems(controller.getCategorias());
			cbCategorias.select(controller.getCategoriaIndex());		
		}
		
		if(!loadingPartial || controller.isContaPartialLoading()){
			String[] contas = controller.getContas();
			cbConta.setItems(contas);
			cbContaTransf.setItems(contas);
			
			cbConta.select(controller.getContaIndex());
			cbContaTransf.select(controller.getContaTransferenciaIndex());
		}
		
		if(!loadingPartial || controller.isFormaPgtoPartialLoading()){
			cbFormaPgto.setItems(controller.getFormasPagamento());
			cbFormaPgto.select(controller.getFormaPgtoIndex());
		}
		
		if(!loadingPartial || controller.isCartaoLoading()){
			cbCartao.setItems(controller.getCartoes());
			cbCartao.select(controller.getCartaoIndex());
		}
		
		updateFormaPgtoControls();

		checkParc();
		
		if(loadingPartial)
			controller.stopPartialLoading();
		
			btnSave.setEnabled(!pago);
			btnConfig1.setEnabled(!pago && !editingParcel);
			btnConfig2.setEnabled(!pago && !editingParcel);
			btConfCartao.setEnabled(!pago && !editingParcel);
			btConfContaTransf.setEnabled(!pago && !editingParcel);
			chkVinc.setEnabled(controller.isVincular());
			btHelpChkVinc.setEnabled(controller.isVincular());

	}

	protected DisposeListener getMeDisposeListener(){
		return this;
	}
	
	protected Control getMeControl(){
		return this;
	}

	protected void initialize(){

		GridLayout layout = new GridLayout(4,false);
		setLayout(layout);

		Image docSettings = ResourceUtil.getImage(getDisplay(), "document-settings-icon.png");
		
		Label label = new Label(this,SWT.NONE);
		label.setText(bundle.getString("lancamento.edit.label.dtpg"));

		GridData gridData = new GridData();
		gridData.horizontalAlignment = GridData.END;
		label.setLayoutData(gridData);

		dtPgto = new DateTime(this,SWT.MEDIUM);
		gridData = new GridData();
		gridData.horizontalSpan = 2;
		dtPgto.setLayoutData(gridData);

		chkFix = new Button(this,SWT.CHECK);
		chkFix.setText(bundle.getString("string.general.fix"));
		
		gridData = new GridData();
		gridData.horizontalAlignment = GridData.BEGINNING;
		
		chkFix.setLayoutData(gridData);
		chkFix.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				controller.setFixPgtDt(chkFix.getSelection());
				controller.getDataPgto().setDate(dtPgto.getYear(), dtPgto.getMonth(), dtPgto.getDay());
			}
		});		
		
		
		label = new Label(this,SWT.NONE);
		label.setText(bundle.getString("lancamento.edit.label.dtdv"));
		
		gridData = new GridData();
		gridData.horizontalAlignment = GridData.END;
		label.setLayoutData(gridData);

		dtDiv = new DateTime(this, SWT.NONE);
		gridData = new GridData();
		gridData.horizontalSpan = 3;
		dtDiv.setLayoutData(gridData);

		label = new Label(this,SWT.NONE);
		label.setText(bundle.getString("lancamento.edit.label.cate"));
		
		gridData = new GridData();
		gridData.horizontalAlignment = GridData.END;		
		label.setLayoutData(gridData);

		cbCategorias = new Combo(this, SWT.NONE | SWT.READ_ONLY);
		gridData = new GridData();
		cbCategorias.setLayoutData(gridData);
		cbCategorias.setItems(controller.getCategorias());
		cbCategorias.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				controller.setCategoriaIndex(cbCategorias.getSelectionIndex());
			}
		});

		btnConfig1 = new Button(this,SWT.PUSH);
		btnConfig1.setImage(docSettings);
		
		gridData = new GridData();
		gridData.horizontalAlignment = GridData.BEGINNING;
		gridData.horizontalSpan = 2;
		btnConfig1.setLayoutData(gridData);
		btnConfig1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				dialogUtil.openCategoriaDialog(getMeDisposeListener());
			}
		});

		label = new Label(this,SWT.NONE);
		label.setText(bundle.getString("lancamento.edit.label.desc"));
		
		gridData = new GridData();
		gridData.horizontalAlignment = GridData.END;
		label.setLayoutData(gridData);

		txtDescr = new Text(this, SWT.BORDER);
		gridData = new GridData();
		gridData.horizontalSpan = 3;
		gridData.widthHint = 325;
		txtDescr.setLayoutData(gridData);

		label = new Label(this,SWT.NONE);
		label.setText(bundle.getString("lancamento.edit.label.val"));
		
		gridData = new GridData();
		gridData.horizontalAlignment = GridData.END;		
		label.setLayoutData(gridData);

		txtVal = new Text(this, SWT.BORDER);
		gridData = new GridData();
		gridData.horizontalSpan = 3;
		gridData.widthHint = 200;
		txtVal.setLayoutData(gridData);

		label = new Label(this,SWT.NONE);
		label.setText(bundle.getString("lancamento.edit.label.cc"));
		
		gridData = new GridData();
		gridData.horizontalAlignment = GridData.END;
		label.setLayoutData(gridData);

		cbConta = new Combo(this, SWT.NONE | SWT.READ_ONLY);
		gridData = new GridData();
		cbConta.setLayoutData(gridData);
		cbConta.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				controller.setContaIndex(cbConta.getSelectionIndex());
			}
		});
		
		btnConfig2 = new Button(this,SWT.PUSH);
		btnConfig2.setImage(docSettings);
		btnConfig2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				dialogUtil.openContaDialog(getMeDisposeListener());
			}
		});

		gridData = new GridData();
		gridData.horizontalAlignment = GridData.BEGINNING;
		gridData.horizontalSpan = 2;
		btnConfig2.setLayoutData(gridData);

		
		label = new Label(this,SWT.NONE);
		label.setText(bundle.getString("lancamento.edit.label.fopg"));
		
		gridData = new GridData();
		gridData.horizontalAlignment = GridData.END;		
		label.setLayoutData(gridData);

		cbFormaPgto = new Combo(this, SWT.NONE | SWT.READ_ONLY);
		gridData = new GridData();
		gridData.horizontalSpan = 3;
		cbFormaPgto.setLayoutData(gridData);

		cbFormaPgto.addSelectionListener(new SelectionAdapter() {

			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				controller.setFormaPgtoIndex(cbFormaPgto.getSelectionIndex());
				updateFormaPgtoControls();
			}
		} );

		lblCartao = new Label(this,SWT.NONE);
		lblCartao.setText(bundle.getString("lancamento.edit.label.creditcard"));
		
		gridData = new GridData();
		gridData.horizontalAlignment = GridData.END;		
		lblCartao.setLayoutData(gridData);

		cbCartao = new Combo(this, SWT.NONE | SWT.READ_ONLY);

		gridData = new GridData();
		cbCartao.setLayoutData(gridData);

		btConfCartao = new Button(this,SWT.PUSH);
		btConfCartao.setImage(docSettings);
		
		gridData = new GridData();
		gridData.horizontalSpan = 2;
		gridData.horizontalAlignment = GridData.BEGINNING;
		btConfCartao.setLayoutData(gridData);
		btConfCartao.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				dialogUtil.openCartaoDialog(getMeDisposeListener());
			}
		});
		
		lblContaTransf = new Label(this,SWT.NONE);
		lblContaTransf.setText(bundle.getString("lancamento.edit.label.tranf-to"));
		
		gridData = new GridData();
		gridData.horizontalAlignment = GridData.END;		
		lblContaTransf.setLayoutData(gridData);

		cbContaTransf = new Combo(this, SWT.NONE | SWT.READ_ONLY);
		gridData = new GridData();
		cbContaTransf.setLayoutData(gridData);

		btConfContaTransf = new Button(this,SWT.PUSH);
		btConfContaTransf.setImage(docSettings);
		btConfContaTransf.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				dialogUtil.openContaDialog(getMeDisposeListener());
			}
		});

		gridData = new GridData();
		gridData.horizontalSpan = 2;
		btConfContaTransf.setLayoutData(gridData);
		
		label = new Label(this, SWT.SEPARATOR | SWT.HORIZONTAL);
		
		gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.horizontalSpan = 4;
		label.setLayoutData(gridData);
		
		Group group = new Group(this,SWT.NONE);
		group.setText(bundle.getString("lancamento.edit.parc.group.title"));
		GridLayout groupLayout = new GridLayout(2,true);
		group.setLayout(groupLayout);
		
		gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.horizontalSpan = 4;
		group.setLayoutData(gridData);
		
		chkMesmoValor = new Button(group,SWT.CHECK);
		chkMesmoValor.setText("Com o mesmo valor.");
		chkMesmoValor.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(chkMesmoValor.getSelection()){
					chkDividir.setSelection(false);
					txtNumPar.setEnabled(true);
					chkVinc.setEnabled(true);
					btHelpChkVinc.setEnabled(true);
				}else{
					txtNumPar.setEnabled(false);
					chkVinc.setEnabled(false);
					chkVinc.setSelection(false);
					btHelpChkVinc.setEnabled(false);
				}
			}
		});
		
		gridData = new GridData();
		gridData.horizontalAlignment = GridData.CENTER;
		chkMesmoValor.setLayoutData(gridData);
		
		chkDividir = new Button(group,SWT.CHECK);
		chkDividir.setText("Dividir valor");
		chkDividir.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(chkDividir.getSelection()){
					chkMesmoValor.setSelection(false);
					txtNumPar.setEnabled(true);
					chkVinc.setEnabled(true);
					btHelpChkVinc.setEnabled(true);
				}else{
					txtNumPar.setEnabled(false);
					chkVinc.setEnabled(false);
					chkVinc.setSelection(false);
					btHelpChkVinc.setEnabled(false);
				}
			}
		});
		
		gridData = new GridData();
		gridData.horizontalAlignment = GridData.CENTER;
		chkDividir.setLayoutData(gridData);

		label = new Label(group,SWT.NONE);
		label.setText(bundle.getString("lancamento.edit.label.num-parcs"));
		label.setAlignment(SWT.RIGHT);
		
		gridData = new GridData();
		gridData.horizontalAlignment = GridData.END;
		label.setLayoutData(gridData);
		
		txtNumPar = new Text(group, SWT.BORDER);
		txtNumPar.setEnabled(false);

		gridData = new GridData();
		gridData.horizontalAlignment = GridData.BEGINNING;
		gridData.widthHint = 50;
		txtNumPar.setLayoutData(gridData);

		chkVinc = new Button(group,SWT.CHECK);
		chkVinc.setText(bundle.getString("lancamento.edit.label.vinc"));
		chkVinc.setEnabled(false);
		chkVinc.pack();
		
		gridData = new GridData();
		gridData.horizontalAlignment = GridData.END;
		chkVinc.setLayoutData(gridData);
		
		btHelpChkVinc = new Button(group, SWT.PUSH);
		btHelpChkVinc.setImage(ResourceUtil.getImage(getDisplay(), "help-icon.png"));
		btHelpChkVinc.setEnabled(false);
		btHelpChkVinc.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				dialogUtil.OpenHelpDialog(bundle.getString("lanc-vinc-help-file"));
			}
		});
		
		gridData = new GridData();
		gridData.horizontalAlignment = GridData.BEGINNING;
		btHelpChkVinc.setLayoutData(gridData);
		
		label = new Label(this,SWT.NONE);
		
		gridData = new GridData();
		gridData.horizontalAlignment = GridData.END;		
		label.setLayoutData(gridData);
		label.setText(bundle.getString("lancamento.edit.label.pgok"));

		chkPg = new Button(this,SWT.CHECK);
		chkPg.setText(bundle.getString("string.general.yes"));
		gridData = new GridData();
		gridData.horizontalSpan = 3;
		chkPg.setLayoutData(gridData);

		label = new Label(this,SWT.SEPARATOR | SWT.HORIZONTAL);
		gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.horizontalSpan = 4;
		label.setLayoutData(gridData);

		Composite composite = new Composite(this,SWT.NONE);
		composite.setLayout(new RowLayout(SWT.HORIZONTAL));

		gridData = new GridData();
		gridData.horizontalSpan = 4;
		composite.setLayoutData(gridData);		

		btnSave = new Button(composite,SWT.PUSH);
		btnSave.setText(bundle.getString("string.general.save"));
		btnSave.setLayoutData(new RowData());
		btnSave.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {

				controller.getDataPgto().setDate(dtPgto.getYear(),dtPgto.getMonth(), dtPgto.getDay());
				controller.getDataDiv().setDate(dtDiv.getYear(),dtDiv.getMonth(), dtDiv.getDay());
				controller.setCategoriaIndex(cbCategorias.getSelectionIndex());
				controller.setDescricao(txtDescr.getText());
				controller.setValor(txtVal.getText());
				controller.setContaIndex(cbConta.getSelectionIndex());
				controller.setContaTransferenciaIndex(cbContaTransf.getSelectionIndex());
				controller.setFormaPgtoIndex(cbFormaPgto.getSelectionIndex());
				controller.setCartaoIndex(cbCartao.getSelectionIndex());
				controller.setParcelas(txtNumPar.getText());
				controller.setParcelado(chkDividir.getSelection());
				controller.setReplicado(chkMesmoValor.getSelection());
				controller.setpago(chkPg.getSelection());
				controller.setVincular(chkVinc.getSelection());

				try{
					controller.salvar();
					fillData();
				}catch(StatizoException ex){
					messageUtil.showMessage(ex, getShell());
				}

			}
		});

		Button button = new Button(composite,SWT.PUSH);
		button.setText(bundle.getString("string.general.cancel"));
		button.setLayoutData(new RowData());
		final LancamentoComposite lancComp = this;
		button.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				lancComp.getParent().dispose();
			}
		});
		
		button = new Button(composite,SWT.PUSH);
		button.setText(bundle.getString("string.general.add"));
		
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				controller.clearSelection();
				fillData();
			}
		});

	}

	protected void updateFormaPgtoControls() {

		boolean isCartao = controller.isCartao();
		boolean isTransferencia = controller.isTransferencia();
		
		lblCartao.setEnabled(isCartao);
		cbCartao.setEnabled(isCartao);
		btConfCartao.setEnabled(isCartao);
		
		lblContaTransf.setEnabled(isTransferencia);
		cbContaTransf.setEnabled(isTransferencia);
		btConfContaTransf.setEnabled(isTransferencia);
		
		if(isCartao){
			cbContaTransf.select(0);
		}else if(isTransferencia){
			cbCartao.select(0);
		}
		
	}

	@Override
	public void widgetDisposed(DisposeEvent e) {

		if(e.widget instanceof CategoriaComposite){
			controller.partialLoad(LancamentoController.LOAD_PARTIAL_CATEGORIA);
		}else if(e.widget instanceof ContaCorrenteComposite){
			controller.partialLoad(LancamentoController.LOAD_PARTIAL_CONTA);
		}else if(e.widget instanceof CartaoCreditoComposite){
			controller.partialLoad(LancamentoController.LOAD_PARTIAL_CARTAO);
		}
		//			else if(e.widget instanceof FormaPagamentoComposite){
		//			controller.partialLoad(LancamentoController.LOAD_PARTIAL_FORMA_PGTO);
		//		}

		fillData();
	}

}